package com.jdbc_.preparedstatement;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;

// 预处理增删改
public class Demo2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入待删除的管理员名字：");
        String admin_name = scanner.nextLine();  // 不用next()，因为当它接收到空格时就默认结束了

        // 读取配置文件信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\com\\jdbc_\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        Class.forName(driver);  // 注册驱动
        Connection connection = DriverManager.getConnection(url, user, password);  // 获取连接

        // 添加
        //String sql = "insert into admin values(?,?)";
        // 修改
        //String sql = "update admin set pwd=? where name=?";
        // 删除
        String sql = "delete from admin where name=?";


        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, admin_name);
        int rows = preparedStatement.executeUpdate();  // 返回的是受影响的行数

        System.out.println(rows > 0 ? "删除成功" : "删除失败");

        preparedStatement.close();
        connection.close();
    }
}
